<?php

require_once('../../libs/smarty.inc' );
require_once('../../libs/smt_variables.php' );
require_once('../../libs/Class_DB.php' );
require_once('../../libs/Class_ERROR.php' );
require_once('../../libs/login.inc.php' );

if( !in_array( "M", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;
$mystaff_id = $_SESSION['ADMIN_LOGIN']['STAFF_ID'] ;

if($_SERVER['REQUEST_METHOD'] == 'GET'){
	
	$db = new Class_DB;
	//SQLクエリー
	//1作成中  2 承認依頼中    3承認済み   4 承認却下    5客先OK  6客先NG
	$sql = "SELECT * FROM p_consumer A,p_consumer_head B where A.consumer_id = B.consumer_id ORDER BY price_no DESC";	
	$res = $db->query($sql);
	$a=0;
	$b=0;
	$c=0;
	while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
		$row['m_date']=$row['m_date']==NULL?"":date('Y/m/d',strtotime($row['m_date']));
		$row['r_date']=$row['r_date']==NULL?"":date('Y/m/d',strtotime($row['r_date']));
		$row['u_date']=$row['u_date']==NULL?"":date('Y/m/d',strtotime($row['u_date']));
		$pricelist[] = $row;
		if($row['status']==1 && $row['staff_id']==$mystaff_id ) $a++;
		if($row['status']==3 && $row['staff_id']==$mystaff_id) $b++;
		if($row['status']==4 && $row['staff_id']==$mystaff_id) $c++;
		
	}
	
	$sql = "SELECT * FROM M_STAFF";
	$res = $db->query($sql);
	
	while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
		$staff[$row['staff_id']] = $row['staff_name'];
		
	}
	
	$smarty->assign('staff', $staff);
	$smarty->assign('a', $a);
	$smarty->assign('b', $b);
	$smarty->assign('c', $c)	;
	$smarty->assign('mystaff_id', $mystaff_id);
	$smarty->assign('pricelist', $pricelist);
}

if($_SERVER['REQUEST_METHOD'] == 'POST'){
	$mod = $_POST['mode'];
	$db = new Class_DB;
	if( $mod == 'update'){
		$date = date('Y/m/d G:i:s');
		$price_nos 		= $_POST['price_no'];
		$status_reasons	= $_POST['status_reason'];
		for($i=0;$i<count($price_nos);$i++){
			$price_no = $price_nos[$i];
			if(isset($_POST['status'.$price_no])){
				$status = $_POST['status'.$price_no];
				$status_reason = $status_reasons[$i];
				$sql = "UPDATE p_consumer_head SET status=$status,status_reason='$status_reason',examiner=$mystaff_id,e_date='$date' WHERE price_no='$price_no'";
				$db->query($sql);
			}
		}
		$db->close();
	}
	$phase = 'complete';
	$smarty->assign('phase', $phase);
}
		

$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );

$smarty->assign('menu_html', $menu_html);
if(in_array( "O", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] )){
$smarty->assign('yes', "1");
}else{
$smarty->assign('yes', "0");
}
$smarty->display('web-admin/price_task.html');


?>